'use strict';

angular.module('app')
    .controller('detailController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "应付款详情";
            var id = $state.params.id;
            $scope.title = $rootScope.title = title;
            $scope.loading = true;
            activate(id);
            // 初始化页面
            function activate(id) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name+'/extractApply/read/detail',
                    data: {'id': id}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.checkrt = function(id,amount,userIdS){
                $scope.loading = true
                $scope.isDisabled = true;//提交disabled
                $scope.alertMessage({
                    text: "确定做此操作吗？", //内容
                    btnCancel: true, //取消按钮
                    btnConfirm: true //确认按钮
                }).then(function () {
                $.ajax({
                    url:$scope.app.project_name + '/extractApply/update/checkrt',
                    data: {'id': id,'amount': amount,'userIdS': userIdS}
                }).then(function (result) {
                    $scope.loading = false;
                    if (result && result.httpCode == 200) {//成功
                        if(result.data>0){
                            $scope.alertModal({"content":"操作成功"});
                        }else {
                            $scope.alertModal({"content":"操作失败"});
                        }
                    } else {
                        $scope.alertModal({"content":"操作失败"});
                    }
                    $state.go("main.finance.extractApply.list");
                    $scope.$apply();
                });
                });
            };

            $scope.checker = function(id,amount,userIdS,status){
                $scope.loading = true
                $scope.isDisabled = true;//提交disabled
                $scope.loading = true
                $scope.isDisabled = true;//提交disabled
                var remark=$('#remark').val();
                if(status==3){
                    if(remark==null||remark==''){
                        $scope.alertModal({"content": "请填写拒绝理由！"});
                        return;
                    }
                    $.ajax({
                        url:$scope.app.project_name + '/extractApply/update/checker',
                        data: {'id': id,'amount': amount,'userIdS': userIdS,'status': status,remark:remark}
                    }).then(function (result) {
                        $scope.loading = false;
                        if (result && result.httpCode == 200) {//成功
                            if(result.data>0){
                                $scope.alertModal({"content":"操作成功"});
                            }else {
                                $scope.alertModal({"content":"操作失败"});
                            }
                        } else {
                            $scope.alertModal({"content":"操作失败"});
                        }
                        $state.go("main.finance.extractApply.list");
                        $scope.$apply();
                    });
                }else{
                $scope.alertMessage({
                    text: "确定做此操作吗？", //内容
                    btnCancel: true, //取消按钮
                    btnConfirm: true //确认按钮
                }).then(function () {
                $.ajax({
                    url:$scope.app.project_name + '/extractApply/update/checker',
                    data: {'id': id,'amount': amount,'userIdS': userIdS,'status': status,remark:remark}
                }).then(function (result) {
                    $scope.loading = false;
                    if (result && result.httpCode == 200) {//成功
                        if(result.data>0){
                            $scope.alertModal({"content":"操作成功"});
                        }else {
                            $scope.alertModal({"content":"操作失败"});
                        }
                    } else {
                        $scope.alertModal({"content":"操作失败"});
                    }
                    $state.go("main.finance.extractApply.list");
                    $scope.$apply();
                });
                });
                }
            };

            $scope.saveExtractPay = function () {
                if($scope.extract.tradeNo==""||$scope.extract.tradeNo==null){
                    $scope.alertModal({"content":"交易流水号不能为空"});
                    $('#fukuan').modal('hide');
                    return;
                }
                if($scope.extract.payDate==""||$scope.extract.payDate==null){
                    $scope.alertModal({"content":"打款时间不能为空"});
                    $('#fukuan').modal('hide');
                    return;
                }
                $.ajax({
                    url : $scope.app.project_name+'/update/pay',
                    data: $scope.extract
                }).then(function(result) {
                    alert(result);
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        if(result.data>0){
                            $('#fukuan').modal('hide');
                            $scope.alertModal({"content":"付款成功"});
                        }else {
                            $('#fukuan').modal('hide');
                            $scope.alertModal({"content":"付款失败"});
                        }
                    } else {
                        $('#fukuan').modal('hide');
                        $scope.alertModal({"content":"付款失败"});
                    }
                    $state.go("main.finance.extractApply.list");
                    $scope.$apply();
                });
            }
            $scope.preview = function(){
                $scope.firstApply = true;
            }
        }]);